// pages/my_car/my_car.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carList:[],
    index:'',
  },
  // 点击注销车辆
  carsTap:function(e){
    let car_id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    //假删除
    let cars = this.data.carList
    cars.splice(index,1)
    this.setData({
      carList:cars
    })
    //真删除
    wx.request({
      url: 'http://121.37.209.61:8888/car/delete', 
      data: {
        car_id:car_id
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        if(res.data.code==200){
          wx.showToast({
            title: '注销成功',
            icon: 'success',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: '注销失败',
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    // 监听窗口高度
    let user_id = wx.getStorageSync('user').user_id
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight
        })
      },
    })
    //请求我的车辆数据
    let that = this
    wx.request({
      url: 'http://121.37.209.61:8888/car/findByUid',
      method:'GET',
      data: {
        user_id: user_id,   
      },
      success (res) {
        let carList = res.data.data
        that.setData({
          carList:carList
        })
      }
    })
  },
})